package TAIC.Classifier;

public class KL_Divergence extends Divergence {

	@Override
	public double cale(double[] x, double[] y) {
		int min = Math.min( x.length , y.length );
		double result = 0 ;
		for ( int i = 0 ; i < min ; i ++ ) 
			result += x [ i ] * Math.log( x[ i ]/ y [ i ]) ;
		return result ;
	}

	public void normalize ( double p [] ) {
		int VECSIZE = p.length ; 
		double total = 0.0 ; 
		double eps = 0.001 ; 
		for ( int i = 0; i < VECSIZE ; i ++) total += p [ i ] ;
		for ( int i = 0 ; i < VECSIZE ; i ++ ) p [ i ] = ( 1 +p [ i ]) / (total +  VECSIZE ); 
		//for ( int i = 0 ; i < VECSIZE ; i ++ ) p [ i ] = ( 1- eps ) * p [ i ] / total + eps / VECSIZE ; 
	}
	
}
